/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */
/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package smartgrid;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;

import java.io.Serializable;
import java.util.GregorianCalendar;

public class DataLine implements Serializable, Comparable<DataLine> {

    public DataLine() {
    }

    public static DataLine loadDataLineByORMID(int ID) {
        try {
            PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
            return loadDataLineByORMID(session, ID);
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine getDataLineByORMID(int ID) {
        try {
            PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
            return getDataLineByORMID(session, ID);
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine loadDataLineByORMID(int ID, org.hibernate.LockMode lockMode) {
        try {
            PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
            return loadDataLineByORMID(session, ID, lockMode);
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine getDataLineByORMID(int ID, org.hibernate.LockMode lockMode) {
        try {
            PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
            return getDataLineByORMID(session, ID, lockMode);
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine loadDataLineByORMID(PersistentSession session, int ID) {
        try {
            return (DataLine) session.load(smartgrid.DataLine.class, new Integer(ID));
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine getDataLineByORMID(PersistentSession session, int ID) {
        try {
            return (DataLine) session.get(smartgrid.DataLine.class, new Integer(ID));
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine loadDataLineByORMID(PersistentSession session, int ID, org.hibernate.LockMode lockMode) {
        try {
            return (DataLine) session.load(smartgrid.DataLine.class, new Integer(ID), lockMode);
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine getDataLineByORMID(PersistentSession session, int ID, org.hibernate.LockMode lockMode) {
        try {
            return (DataLine) session.get(smartgrid.DataLine.class, new Integer(ID), lockMode);
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine[] listDataLineByQuery(String condition, String orderBy) {
        try {
            PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
            return listDataLineByQuery(session, condition, orderBy);
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine[] listDataLineByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) {
        try {
            PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
            return listDataLineByQuery(session, condition, orderBy, lockMode);
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine[] listDataLineByQuery(PersistentSession session, String condition, String orderBy) {
        StringBuffer sb = new StringBuffer("From smartgrid.DataLine as DataLine");
        if (condition != null) {
            sb.append(" Where ").append(condition);
        }
        if (orderBy != null) {
            sb.append(" Order By ").append(orderBy);
        }
        try {
            Query query = session.createQuery(sb.toString());
            List list = query.list();
            return (DataLine[]) list.toArray(new DataLine[list.size()]);
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine[] listDataLineByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) {
        StringBuffer sb = new StringBuffer("From smartgrid.DataLine as DataLine");
        if (condition != null) {
            sb.append(" Where ").append(condition);
        }
        if (orderBy != null) {
            sb.append(" Order By ").append(orderBy);
        }
        try {
            Query query = session.createQuery(sb.toString());
            query.setLockMode("this", lockMode);
            List list = query.list();
            return (DataLine[]) list.toArray(new DataLine[list.size()]);
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine loadDataLineByQuery(String condition, String orderBy) {
        try {
            PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
            return loadDataLineByQuery(session, condition, orderBy);
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine loadDataLineByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) {
        try {
            PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
            return loadDataLineByQuery(session, condition, orderBy, lockMode);
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine loadDataLineByQuery(PersistentSession session, String condition, String orderBy) {
        DataLine[] dataLines = listDataLineByQuery(session, condition, orderBy);
        if (dataLines != null && dataLines.length > 0) {
            return dataLines[0];
        } else {
            return null;
        }
    }

    public static DataLine loadDataLineByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) {
        DataLine[] dataLines = listDataLineByQuery(session, condition, orderBy, lockMode);
        if (dataLines != null && dataLines.length > 0) {
            return dataLines[0];
        } else {
            return null;
        }
    }

    public static java.util.Iterator iterateDataLineByQuery(String condition, String orderBy) {
        try {
            PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
            return iterateDataLineByQuery(session, condition, orderBy);
        } catch (Exception e) {
            return null;
        }
    }

    public static java.util.Iterator iterateDataLineByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) {
        try {
            PersistentSession session = smartgrid.SmartgridPersistentManager.instance().getSession();
            return iterateDataLineByQuery(session, condition, orderBy, lockMode);
        } catch (Exception e) {
            return null;
        }
    }

    public static java.util.Iterator iterateDataLineByQuery(PersistentSession session, String condition, String orderBy) {
        StringBuffer sb = new StringBuffer("From smartgrid.DataLine as DataLine");
        if (condition != null) {
            sb.append(" Where ").append(condition);
        }
        if (orderBy != null) {
            sb.append(" Order By ").append(orderBy);
        }
        try {
            Query query = session.createQuery(sb.toString());
            return query.iterate();
        } catch (Exception e) {
            return null;
        }
    }

    public static java.util.Iterator iterateDataLineByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) {
        StringBuffer sb = new StringBuffer("From smartgrid.DataLine as DataLine");
        if (condition != null) {
            sb.append(" Where ").append(condition);
        }
        if (orderBy != null) {
            sb.append(" Order By ").append(orderBy);
        }
        try {
            Query query = session.createQuery(sb.toString());
            query.setLockMode("this", lockMode);
            return query.iterate();
        } catch (Exception e) {
            return null;
        }
    }

    public static DataLine loadDataLineByCriteria(DataLineCriteria dataLineCriteria) {
        DataLine[] dataLines = listDataLineByCriteria(dataLineCriteria);
        if (dataLines == null || dataLines.length == 0) {
            return null;
        }
        return dataLines[0];
    }

    public static DataLine[] listDataLineByCriteria(DataLineCriteria dataLineCriteria) {
        return dataLineCriteria.listDataLine();
    }

    public static DataLine createDataLine() {
        return new smartgrid.DataLine();
    }

    public boolean save() {
        try {
            smartgrid.SmartgridPersistentManager.instance().saveObject(this);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean delete() {
        try {
            smartgrid.SmartgridPersistentManager.instance().deleteObject(this);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean refresh() {
        try {
            smartgrid.SmartgridPersistentManager.instance().getSession().refresh(this);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean evict() {
        try {
            smartgrid.SmartgridPersistentManager.instance().getSession().evict(this);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean deleteAndDissociate() {
        try {
            if (getTariff() != null) {
                getTariff().dataLines.remove(this);
            }

            if (getContract() != null) {
                getContract().dataLines.remove(this);
            }

            return delete();
        } catch (Exception e) {
            return false;
        }
    }

    public boolean deleteAndDissociate(org.orm.PersistentSession session) {
        try {
            if (getTariff() != null) {
                getTariff().dataLines.remove(this);
            }

            if (getContract() != null) {
                getContract().dataLines.remove(this);
            }

            try {
                session.delete(this);
                return true;
            } catch (Exception e) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    private void this_setOwner(Object owner, int key) {
        if (key == smartgrid.ORMConstants.KEY_DATALINE_CONTRACT) {
            this.contract = (smartgrid.Contract) owner;
        } else if (key == smartgrid.ORMConstants.KEY_DATALINE_TARIFF) {
            this.tariff = (smartgrid.Tariff) owner;
        }
    }
    org.orm.util.ORMAdapter _ormAdapter = new org.orm.util.AbstractORMAdapter() {

        public void setOwner(Object owner, int key) {
            this_setOwner(owner, key);
        }
    };
    private int ID;
    private smartgrid.Tariff tariff;
    private smartgrid.Contract contract;
    private java.util.Date timeStamp;
    private double Value;
    private String Unit;
    private short Hora;
    private short Minuto;

    private void setID(int value) {
        this.ID = value;
    }

    public int getID() {
        return ID;
    }

    public int getORMID() {
        return getID();
    }

    public void setTimeStamp(java.util.Date value) {
        this.timeStamp = value;
    }

    public java.util.Date getTimeStamp() {
        return timeStamp;
    }

    public void setValue(double value) {
        this.Value = value;
    }

    public double getValue() {
        return Value;
    }

    public void setUnit(String value) {
        this.Unit = value;
    }

    public String getUnit() {
        return Unit;
    }

    public void setHora(short value) {
        this.Hora = value;
    }

    public short getHora() {
        return Hora;
    }

    public void setMinuto(short value) {
        this.Minuto = value;
    }

    public short getMinuto() {
        return Minuto;
    }

    public void setContract(smartgrid.Contract value) {
        if (contract != null) {
            contract.dataLines.remove(this);
        }
        if (value != null) {
            value.dataLines.add(this);
        }
    }

    public smartgrid.Contract getContract() {
        return contract;
    }

    /**
     * This method is for internal use only.
     */
    private void setORM_Contract(smartgrid.Contract value) {
        this.contract = value;
    }

    private smartgrid.Contract getORM_Contract() {
        return contract;
    }

    public void setTariff(smartgrid.Tariff value) {
        if (tariff != null) {
            tariff.dataLines.remove(this);
        }
        if (value != null) {
            value.dataLines.add(this);
        }
    }

    public smartgrid.Tariff getTariff() {
        return tariff;
    }

    /**
     * This method is for internal use only.
     */
    private void setORM_Tariff(smartgrid.Tariff value) {
        this.tariff = value;
    }

    private smartgrid.Tariff getORM_Tariff() {
        return tariff;
    }

    /**
     * Retorna o objecto serializado no formato JSon.
     */
    public String getJSon() {
        //TODO: Implement Method
        throw new UnsupportedOperationException();
    }

    public String toString() {
        return "" + this.Value;
    }

    @Override
    public int compareTo(DataLine o) {
        int IGUAL = 0;
        int MENOR = 1;
        int MAIOR = -1;
        GregorianCalendar oG = ETL.ETL.postgresDateToGCalendar(o.getTimeStamp());
        GregorianCalendar thisG = ETL.ETL.postgresDateToGCalendar(this.timeStamp);

        if (o.getTimeStamp().equals(this.timeStamp)) {
            if (o.getHora() == this.Hora) {
                if (o.getMinuto() == this.Minuto) {
                    return IGUAL;
                }
                if (o.getMinuto() > this.Minuto) {
                    return MAIOR;
                } else {
                    return MENOR;
                }
            }
            if (o.getHora() > this.Hora) {
                return MAIOR;
            } else {
                return MENOR;
            }
        }
        if (oG.compareTo(thisG) == 1) {
            return MAIOR;
        }else{
            return MENOR;
        }
    }
}
